import { defineStore } from "pinia";

import { findMySupportITSOPUserPager, getCategoryTreeAllData } from "@/api/modules/common";

import { findByCode } from "@/api/modules/changeManagement";
import { getConfigurationCategoryTree } from "@/api/modules/assetManagement";

import { useUserStore } from "./user";

interface OptionsItem {
  cateList: any[];
  orgList: any[];
  statusList: any[];
  positionList: any[];
}

export const allAssetOptions = defineStore({
  id: "asset-options",
  state: (): OptionsItem => {
    return {
      cateList: [], // 资产分类列表
      orgList: [], // 所属客户列表
      statusList: [], // 资产状态列表
      positionList: []
    };
  },
  getters: {},
  actions: {
    async getAllOptions() {
      const userStore = useUserStore();
      // 获取所属客户列表
      await findMySupportITSOPUserPager({
        "itsopUserQueryDTO.loginName": userStore.username,
        sidx: "orgNo",
        sord: "desc",
        page: 1,
        rows: 100
      }).then((res: any) => {
        this.orgList = res.data || [];
      });
      // 获取分类列表
      await getConfigurationCategoryTree({
        userName: userStore.username,
        findAll: true,
        simpleDto: true,
        parentEventId: 0
      }).then((res: any) => {
        this.cateList = [res];
      });
      // 状态列表
      await findByCode({
        "dataDictionaryQueryDto.groupCode": "useStatus"
      }).then((res: any) => {
        this.statusList = res || [];
      });
      // 位置列表
      getCategoryTreeAllData({
        flag: "dataDic",
        types: "Location",
        parentEventId: 0,
        num: 50
      }).then((res: any) => {
        this.positionList = res.children || [];
      });
    }
  }
});
